/*
 * Copyright 2012 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package com.dj.web.utils;

import javax.servlet.http.HttpServletRequest;

import com.dj.domain.entity.DaiJiaUser;
import com.dj.web.entity.DaijiaWebUser;

/**
 * 类DaijiaWebUserUtil.java的实现描述：TODO 类实现描述
 * 
 * @author jiansheng.xujs 2012-5-28 下午2:53:09
 */
public class DaijiaWebUserUtil {

    public static final String DAIJIA_USER_STORE_KEY = "_daijia_session_user_";

    public static boolean isLogon(HttpServletRequest request) {
        DaijiaWebUser webUser = (DaijiaWebUser) request.getSession(true).getAttribute(DAIJIA_USER_STORE_KEY);
        return webUser == null ? false : true;
    }

    /**
     * 取得当前用户信息
     * 
     * @param request
     * @return
     */
    public static DaijiaWebUser getUserFromRequest(HttpServletRequest request) {
        DaijiaWebUser webUser = (DaijiaWebUser) request.getSession(true).getAttribute(DAIJIA_USER_STORE_KEY);
        return webUser;
    }

    /**
     * 保持当前用户信息
     * 
     * @param daijiaUser
     * @param request
     */
    public static void storeUserToSession(DaiJiaUser daijiaUser, HttpServletRequest request) {
        if (daijiaUser != null) {
            DaijiaWebUser webUser = new DaijiaWebUser();
            webUser.setProvince(daijiaUser.getProvince());
            webUser.setCity(daijiaUser.getCity());
            webUser.setCounty(daijiaUser.getCounty());
            webUser.setId(daijiaUser.getId());
            webUser.setMobile(daijiaUser.getMobile());
            webUser.setLoginId(daijiaUser.getLoginId());
            webUser.setEmail(daijiaUser.getEmail());
            webUser.setSex(daijiaUser.getSex());
            webUser.setName(daijiaUser.getName());
            request.getSession(true).setAttribute(DAIJIA_USER_STORE_KEY, webUser);
        }
    }

    /**
     * 保持当前用户信息
     * 
     * @param webUser
     * @param request
     */
    public static void storeUserToSession(DaijiaWebUser webUser, HttpServletRequest request) {
        if (webUser != null) {
            request.getSession(true).setAttribute(DAIJIA_USER_STORE_KEY, webUser);
        }
    }

    /**
     * 清除用户信息
     * 
     * @param request
     */
    public static void removeUserToSession(HttpServletRequest request) {
        request.getSession(true).removeAttribute(DAIJIA_USER_STORE_KEY);
    }

}
